namespace Umbraco.Ecommerce.WebShop.UserControls
{
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using umbraco.cms.businesslogic.member;
    using Umbraco.Ecommerce.BusinessLogic;

    /// <summary>
    ///		Summary description for recentorders.
    /// </summary>
    public class RecentOrders : System.Web.UI.UserControl
    {
        protected System.Web.UI.WebControls.Repeater orderrpt;

        public Member ThisMember = null;
        private void Page_Load(object sender, System.EventArgs e)
        {
            try
            {
                if (ThisMember != null)
                {
                    Member m = ThisMember;
                    string orders = m.getProperty("orders").Value.ToString();

                    string[] orderids = orders.Split(',');

                    DataTable dt = new DataTable();
                    dt.Columns.Add("orderid");
                    dt.Columns.Add("status");
                    dt.Columns.Add("order");

                    foreach (string orderid in orderids)
                    {
                        if (orderid.Length > 0)
                        {
                            Order o = new Order(int.Parse(orderid));
                            DataRow dr = dt.NewRow();
                            dr["orderid"] = orderid;
                            dr["order"] = o.SessionId + "_" + orderid + ".pdf";
                            dt.Rows.Add(dr);
                        }
                    }
                    orderrpt.DataSource = dt;
                    orderrpt.DataBind();

                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
            // Put user code to initialize the page here
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            orderrpt.ItemDataBound += new RepeaterItemEventHandler(orderrpt_ItemDataBound);
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        ///		Required method for Designer support - do not modify
        ///		the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        private void orderrpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            System.Web.UI.HtmlControls.HtmlAnchor a = (System.Web.UI.HtmlControls.HtmlAnchor)e.Item.FindControl("orderlink");
            string order = ((DataRowView)e.Item.DataItem).Row["order"].ToString();

            a.HRef = "/data/" + order;
        }
    }
}
